﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace OverMouse
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        public Graphics gMouse;
        public Rectangle rMouse;
        public Rectangle rScreen;

        static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        const UInt32 SWP_NOSIZE = 0x0001;
        const UInt32 SWP_NOMOVE = 0x0002;
        const UInt32 SWP_SHOWWINDOW = 0x0040;

        public String sScreen;

        public Form1()
        {
            InitializeComponent();
            sScreen = File.ReadAllText("Screen.ini").Trim().ToLowerInvariant();
            foreach (Screen s in Screen.AllScreens)
                if (s.DeviceName.ToLowerInvariant().Trim() == sScreen)
                    rScreen = s.Bounds;
            if (rScreen == null)
                Application.Exit();
            this.toolStripMenuItem1.Click += toolStripMenuItem1_Click;
            this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
            gMouse = CreateGraphics();
            rMouse = new Rectangle(new Point(0, 0), new Size(Cursor.Size.Width, Cursor.Size.Height));
            BackColor = Color.Lime;
            TransparencyKey = Color.Lime;
        }

        void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (
                Cursor.Position.X > rScreen.X &&
                Cursor.Position.Y > rScreen.Y &&
                Cursor.Position.X < rScreen.X + rScreen.Width &&
                Cursor.Position.Y < rScreen.Y + rScreen.Height)
            {
                Left = Cursor.Position.X + 1;
                Top = Cursor.Position.Y + 1;
            }
            else
            {
                Left = -Cursor.Size.Width;
                Top = -Cursor.Size.Height;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            Cursor.Draw(gMouse, rMouse);
            SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
        }
    }
}
